home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7263 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.2 KB  |  83 lines

  1. Path: pegasus.montclair.edu!harmon
  2. From: harmon@pegasus.montclair.edu (Derek Harmon)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Can someone help me with fopen
  5. Date: 14 Feb 1996 09:05:50 -0500
  6. Organization: Montclair State University
  7. Message-ID: <harmon.824306709@pegasus.montclair.edu>
  8. References: <4fouoo$k0e@news.uncc.edu>
  9. NNTP-Posting-Host: pegasus.montclair.edu
  10. X-Newsreader: NN version 6.5.0 #68 (NOV)
  11.  
  12. In comp.lang.c you write:
  13.  
  14. >Here is my problem.  I am trying to read a text file, search for a certain
  15. >string and write that string to an outfile.  I am having trouble with the fopen
  16. >commnad. Here is a fragment of my code.
  17.  
  18. >char input; /* variable to put filename in*/
  19. >FILE *infile /* Pointer to File to be used as input */
  20.  
  21. : char input[13];
  22.  
  23.    input needs to be either a statically allocated array (as shown here, a
  24. length of 13 is suitable for a DOS filename w/o path, vary as required) or
  25. a pointer to a char in which case you must allocate with malloc().
  26.  
  27. >if (argv[argc] == "-i")
  28. >       {input = ((argv[argc]) + 1);
  29. >        infile = fopen((("%s", input), "r"));
  30. >        if ((infile = fopen((("%s", input), "r"))) == NULL)
  31. >               {/*Error Message*/
  32.  
  33.    First, delete line #3 here as there is no need to open a file twice, and
  34. it is best done inside the if that tests for error conditions.  Also be aware
  35. that argv[argc] is guaranteed to be NULL.  argc is 1 for the path/name of the
  36. executing program, held in argv[0].  When argc is 3, argv[2] is the last
  37. argument from the command-line.  Even if argv[] were the string you wanted,
  38. you cannot compare strings with the == operator, you must use strcmp().  For
  39. example, if (!strcmp(argv[1],"-i")) ... be aware that strcmp(a,b) returns 0
  40. when string a is equal to string b.
  41.  
  42. >I keep getting an "too few arguments to function `fopen'
  43.  
  44.    The prototype for fopen() is,
  45.  
  46.    FILE *fopen(const char *filename, const char *mode);
  47.  
  48.    First, "%s" belongs with printf() and scanf() functions and their kin, and
  49. is what is known as a format specifier in the language.  It will associate
  50. only with functions that take a variable number of arguments.  fopen() is
  51. not such a function.  What you should use,
  52.  
  53.    if ((infile = fopen("\\projects\\foo.dat", "rt")) == NULL) {
  54.  
  55.    The first parameter is a filename.  It can be a char * or array of char,
  56.  
  57.    char filename[13];
  58.    :
  59.    if ((infile = fopen(filename, "wb+")) == NULL) {
  60.  
  61.    char *fname;
  62.    :
  63.    fname = (char *)malloc(24 * sizeof(char));
  64.    strcpy(fname,"\\graphics\\tiger.jpg");
  65.    if ((infile = fopen(fname, "rb")) == NULL) {
  66.  
  67.    There are a variety of modes, read/write, text/binary, open for update (+),
  68. that should be covered in any good manual or reference on C.
  69.  
  70. >Please e-mail me is you have any idea on this problem(or any other aspect of
  71. >       this program for that matter :>)
  72. >Any help would be greatly appreciated
  73.  
  74.    I tried to respond via E-Mail, but your address lacked a domain.
  75.  
  76.                                                    -- Stone
  77. --
  78. # Derek Harmon (aka Stonelight)    harmon@pegasus.montclair.edu
  79. # - Computer Science Undergrad, Montclair State University, NJ
  80. # - My views are my own, nobody else is this creative.  3;)>
  81. ... C programmer run, C programmer crash, Ada programmer.
  82.  
  83.